package com.yitongmao.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;

import com.google.gson.Gson;
import com.yitongmao.AppConstancts;
import com.yitongmao.adapter.ReDaiDatailsAdapter;
import com.yitongmao.bean.HomeListBean;
import com.yitongmao.common.CallBackListener;
import com.yitongmao.core.network.service.BaseService;
import com.yitongmao.util.SignUtil;
import com.yitongmao.yitmfruit.R;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;

public class DatailsActivity extends AppCompatActivity {

    private AutoLinearLayout mRedaihuitui;
    private ListView searchListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immerSion();
        setContentView(R.layout.activity_datails);
        initView();
        setOnClick();
        setNetWork();
    }
    //找到视图控件
    private void initView() {
        mRedaihuitui = (AutoLinearLayout) findViewById(R.id.mRedaihuitui);
        searchListView = (ListView) findViewById(R.id.searchListView);
    }
    //点击事件
    private void setOnClick(){
        mRedaihuitui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              DatailsActivity.this.finish();
            }
        });
    }
    //请求
    public void setNetWork() {
        Map params = new TreeMap();
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = String.valueOf(new Random().nextInt());
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("sign", SignUtil.sign(params)); //这里也需要时完整的url
        BaseService.getInstance().simplePostRequest(AppConstancts.Net.homeUrl, params, new CallBackListener() {
            private List<HomeListBean.GoodsListBean> goodsList;
            private HomeListBean listBean;
            @Override
            public void onStart() {
            }
            @Override
            public void onSuccess(Object result) {
                Log.e("zzz",result.toString());
                String string = (String) result;
                Gson gson=new Gson();
                listBean = gson.fromJson(string, HomeListBean.class);
                List<HomeListBean.GoodsListBean> goodsList = listBean.goodsList;
                if (goodsList.equals("")||goodsList.size()<0){
                    Log.e("zzz","goodsList"+goodsList);
                }else {
                    searchListView.setAdapter(new ReDaiDatailsAdapter(DatailsActivity.this,goodsList));
                }
            }
            @Override
            public void onFail(Object result) {
                System.out.print("失败");
            }
        });
    }
    //沉浸式状态栏
    public void immerSion() {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }
    }
}
